using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace CMS_SUN_IF_Extraction
{
    class DAL
    {
        private string connectionString;
        //private SqlConnection con;
        private SqlConnection con;
        //private SqlCommandBuilder cmbd;
        //SqlDataAdapter adap;
        public bool connect()
        {
            //connectionString = ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString;
            connectionString = GlobalClass.getConnectionString();
            if (connectionString == "")
            {
                MessageBox.Show("Connection string is null!");
                return false;
            }

            try
            {
                if (con == null)
                {
                    con = new SqlConnection(connectionString);
                    con.Open();
                }
                else if (con.State == ConnectionState.Closed)
                    con.Open();
            }
            catch (SqlException ole)
            {
                MessageBox.Show(ole.Message);
                return false;
            }
            return true;
        }
        public void disconnect()
        {
            try
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            catch (SqlException ole)
            { }
            finally
            {
                con.Close();
            }
        }

        public int executeNonquery(string sql)
        {
            if (connect() == false)
                return -1;
            int rslt = 0;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                rslt = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException se)
            {
                trans.Rollback();
                rslt = -1;
            }
            finally
            {
                disconnect();
            }
            return rslt;
        }

        public DataTable executeQuery(string sql)
        {
            if (connect() == false)
                return null;           
            SqlDataAdapter adap = new SqlDataAdapter(sql, con);            
            DataTable tbl = new DataTable();
            adap.Fill(tbl);
            disconnect();
            return tbl;
        }

        internal int execute_Proc(string pProcname)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = pProcname;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        public int execute_proc(string pProcname, string pVATCenter, string pStartDate, string pEndDate, int pAcct_period)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = pProcname;
            cmd.CommandType = CommandType.StoredProcedure;            
            cmd.Parameters.AddWithValue("@Start_Date", pStartDate);
            cmd.Parameters.AddWithValue("@End_Date", pEndDate);
            cmd.Parameters.AddWithValue("@VATCenter", pVATCenter);
            cmd.Parameters.AddWithValue("@Account_Period", pAcct_period);
            cmd.Parameters["@Start_Date"].DbType = DbType.DateTime;
            cmd.Parameters["@End_Date"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int qSUN_SumSTDCode(string pStartDate, string pEndDate, string pVATCenter, int pAcct_period)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_SUN_SumSTDCode";
            cmd.CommandType = CommandType.StoredProcedure;            
            cmd.Parameters.AddWithValue("@Start_Date", pStartDate);
            cmd.Parameters.AddWithValue("@End_Date", pEndDate);
            cmd.Parameters.AddWithValue("@VATCenter", pVATCenter);
            cmd.Parameters.AddWithValue("@Account_Period", pAcct_period);

            cmd.Parameters["@Start_Date"].DbType = DbType.DateTime;
            cmd.Parameters["@End_Date"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int qSUN_SERV(string pVATCenter, string pStartDate, string pEndDate, int pAcct_period)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_qSUN_Serv";
            cmd.CommandType = CommandType.StoredProcedure;           
            cmd.Parameters.AddWithValue("@Start_Date", pStartDate);
            cmd.Parameters.AddWithValue("@End_Date", pEndDate);
            cmd.Parameters.AddWithValue("@VATCenter", pVATCenter);
            cmd.Parameters.AddWithValue("@Account_Period", pAcct_period);

            cmd.Parameters["@Start_Date"].DbType = DbType.DateTime;
            cmd.Parameters["@End_Date"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int qSUN_VAT(string pVATCenter, string pStartDate, string pEndDate, int pAcct_period)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_SUN_VAT";
            cmd.CommandType = CommandType.StoredProcedure;            
            cmd.Parameters.AddWithValue("@Start_Date", pStartDate);
            cmd.Parameters.AddWithValue("@End_Date", pEndDate);
            cmd.Parameters.AddWithValue("@VATCenter", pVATCenter);
            cmd.Parameters.AddWithValue("@Account_Period", pAcct_period);

            cmd.Parameters["@Start_Date"].DbType = DbType.DateTime;
            cmd.Parameters["@End_Date"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int qSUN_TOTAL(string pVATCenter, string pStartDate, string pEndDate, int pAcct_period)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_SUN_TOTAL";
            cmd.CommandType = CommandType.StoredProcedure;            
            cmd.Parameters.AddWithValue("@Start_Date", pStartDate);
            cmd.Parameters.AddWithValue("@End_Date", pEndDate);
            cmd.Parameters.AddWithValue("@VATCenter", pVATCenter);
            cmd.Parameters.AddWithValue("@Account_Period", pAcct_period);

            cmd.Parameters["@Start_Date"].DbType = DbType.DateTime;
            cmd.Parameters["@End_Date"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal DataTable get_tVAT_Check_Table()
        {
            if (connect() == false)
                return null;
            SqlDataAdapter adap = new SqlDataAdapter("Select * from tVAT_Check_Table",con);
            DataTable tbl = new DataTable();
            adap.Fill(tbl);
            return tbl;
        }

        internal int gAppend_VAT_Check_Table(string pVAT_Center, int pVAT_Start_Num, int pVAT_End_Num)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_gAppend_VAT_Check_Table";
            cmd.CommandType = CommandType.StoredProcedure;            
            cmd.Parameters.AddWithValue("@VATCenter", pVAT_Center);
            cmd.Parameters.AddWithValue("@VAT_Start_Num", pVAT_Start_Num);
            cmd.Parameters.AddWithValue("@VAT_End_Num", pVAT_End_Num);
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int update_last_extract(string pCurrVATCenter, string pStartExtDate, string pEndExtDate, string pLastExtDate)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "sp_update_lastExtract_date";
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@pCurVATCenter", pCurrVATCenter);
            cmd.Parameters.AddWithValue("@pCurStartExtractDate", pStartExtDate);
            cmd.Parameters.AddWithValue("@pCurEndExtractDate", pEndExtDate);
            cmd.Parameters.AddWithValue("@pLastExtractDate", pLastExtDate);

            cmd.Parameters["@pCurStartExtractDate"].DbType = DbType.DateTime;
            cmd.Parameters["@pCurEndExtractDate"].DbType = DbType.DateTime;
            cmd.Parameters["@pLastExtractDate"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException se)
            {
                trans.Rollback();
                MessageBox.Show(se.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int Upd_ImportSUN_IND_newRevenue(string procname, string currDate)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = procname;
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@currDate", currDate);
            cmd.Parameters["@currDate"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException se)
            {
                trans.Rollback();
                MessageBox.Show(se.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }

        internal int Upd_Drug_T2Code(string proc_name, string fromdate, string todate)
        {
            if (connect() == false)
                return -1;
            int num = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = proc_name;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@datefrom", fromdate);
            cmd.Parameters["@datefrom"].DbType = DbType.DateTime;
            cmd.Parameters.AddWithValue("@dateto", todate);
            cmd.Parameters["@dateto"].DbType = DbType.DateTime;
            cmd.Connection = con;
            SqlTransaction trans = con.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                num = cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException se)
            {
                trans.Rollback();
                MessageBox.Show(se.Message);
                num = -1;
            }
            finally
            {
                disconnect();
            }
            return num;
        }
    }
}
